package resoft.jeasyform.components;

import charvax.swing.JLabel;

/**
 * <p></p>
 * User: albert
 * Date: Aug 26, 2007
 * Time: 7:51:51 PM
 */
public class Label extends Component {
    public Label() {

    }

    public Label(String text) {
        setText(text);
    }


    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
        if (jLabel != null) {
            jLabel.setText(text);
            adjustPosition();
        }
    }

    public charva.awt.Component getSwingObject() {
        jLabel = new JLabel(getText());

        adjustPosition();

        jLabel.setVisible(visible);
        return jLabel;
    }

    private void adjustPosition() {
        //get align.can't use alignment of JLabel.it only works on BoxLayout
        int top = getY();
        int left;
        int bottom = getY() + getHeight();
        int right;
        int textLength = getText().length();
        if (getAlign().equalsIgnoreCase("left")) {
            left = getX();
            right = getX() + getWidth();
        } else if (getAlign().equalsIgnoreCase("center")) {
            left = getX() + (getWidth() - textLength) / 2;
            right = left + textLength;
        } else {
            //right alignment
            left = getX() + getWidth() - textLength;
            right = getX() + getWidth();
            //decrease 2 cos window border
            left -= 2;
            right -= 2;
        }
        jLabel.setBounds(new charva.awt.Rectangle(top, left, bottom, right));
    }

    /**
     * hide
     */
    public void hide() {
        if (jLabel != null) {
            jLabel.setVisible(false);
        }
        visible = false;
    }

    /**
     * show
     */
    public void show() {
        if (jLabel != null) {
            jLabel.setVisible(true);
        }
        visible = true;
    }


    private String text;
    private JLabel jLabel;
    private boolean visible = true;

}
